package com.tyk.bookstore.front.payment.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tyk.bookstore.front.order.model.dto.Order4PaymentRecordDto;
import com.tyk.bookstore.front.order.service.OrderApiService;
import com.tyk.bookstore.front.payment.model.entity.PaymentRecordDo;
import com.tyk.bookstore.front.payment.model.query.PaymentRecordPageQuery;
import com.tyk.bookstore.front.payment.model.vo.PaymentRecordPageVo;
import com.tyk.bookstore.front.payment.model.vo.PaymentRecordVo;
import com.tyk.bookstore.front.payment.repository.PaymentRecordRepository;
import com.tyk.bookstore.front.payment.service.PaymentRecordService;
import com.tyk.bookstore.front.payment.util.MapStructPayment;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author tyk
 * @description 支付记录
 */
@Service
@RequiredArgsConstructor

public class PaymentRecordServiceImpl implements PaymentRecordService {

    private final PaymentRecordRepository paymentRecordRepository;
    private final OrderApiService orderApiService;
    private final MapStructPayment mapStructPayment;

    @Override
    public PaymentRecordVo getById(Long id) {
        PaymentRecordDo paymentRecord = paymentRecordRepository.selectById(id);
        return mapStructPayment.toPaymentRecordVo(paymentRecord);
    }

    @Override
    public PaymentRecordPageVo getPage(PaymentRecordPageQuery query) {

        // 分页查询
        Page<PaymentRecordDo> page = new Page<>(query.getPage(), query.getLimit());
        LambdaQueryWrapper<PaymentRecordDo> w = buildPageQueryWrapper(query);
        paymentRecordRepository.selectPage(page, w);
        PaymentRecordPageVo pv = mapStructPayment.toPaymentRecordPageVo(page);

        // 查询订单编号
        Set<Long> oidSet = page.getRecords().stream().map(PaymentRecordDo::getOrderId).collect(Collectors.toSet());
        Map<Long, String> oMap = orderApiService
                .getOrderNoListByIdSet(oidSet)
                .stream().collect(Collectors.toMap(
                        Order4PaymentRecordDto::getId,
                        Order4PaymentRecordDto::getOrderNo
                ));
        pv.getRecords().forEach(it -> {
            it.setOrderNo(oMap.get(it.getOrderId()));
        });

        // 返回
        return pv;

    }

    /**
     * 构建分页查询Wrapper
     */
    private LambdaQueryWrapper<PaymentRecordDo> buildPageQueryWrapper(PaymentRecordPageQuery query) {

        long memberId = StpUtil.getLoginIdAsLong();

        LambdaQueryWrapper<PaymentRecordDo> w = new LambdaQueryWrapper<>();

        w.eq(PaymentRecordDo::getMemberId, memberId);
        w.eq(query.getPaymentWay() != null, PaymentRecordDo::getPaymentWay, query.getPaymentWay());
        w.orderBy(true, false, PaymentRecordDo::getPaymentTime);

        return w;
    }


}

